﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VideoRentalShop.Model;
using System.Data.Entity.Validation;
using System.Data.Entity;
using VideoRentalShop.Controller.Data;

namespace VideoRentalShop.Controller
{
    public class DataSource
    {
        private static DataSource _instance;
        public static DataSource Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new DataSource();
                }

                return _instance;
            }
        }


        private MovieDbContext _db;
        private ClientDataSource _client;
        private GenreDataSource _genre;
        private GenreDetailsDataSource _genreDetails;
        private MovieDataSource _movie;
        private OrderDataSource _order;
        private OrderDetailsDataSource _orderDetails;
        //private static readonly int MONTHS_TO_REMOVE = 6;

        public ClientDataSource Client
        {
            get
            {
                if (_client == null)
                {
                    _client = new ClientDataSource(_db, Order);
                }

                return _client;
            }
        }
        public GenreDetailsDataSource GenreDetails
        {
            get
            {
                if (_genreDetails == null)
                {
                    _genreDetails = new GenreDetailsDataSource(_db);
                }

                return _genreDetails;
            }
        }
        public GenreDataSource Genre
        {
            get
            {
                if (_genre == null)
                {
                    _genre = new GenreDataSource(_db, GenreDetails);
                }

                return _genre;
            }
        }
        public MovieDataSource Movie
        {
            get
            {
                if (_movie == null)
                {
                    _movie = new MovieDataSource(_db, OrderDetails, GenreDetails);
                }

                return _movie;
            }
        }
        public OrderDataSource Order
        {
            get
            {
                if (_order == null)
                {
                    _order = new OrderDataSource(_db, OrderDetails);
                }

                return _order;
            }
        }
        public OrderDetailsDataSource OrderDetails
        {
            get
            {
                if (_orderDetails == null)
                {
                    _orderDetails = new OrderDetailsDataSource(_db);
                }

                return _orderDetails;
            }
        }


        public void CleanDatabase()
        {
            var date = DateTime.Now.AddYears(-2);
            foreach (var client in _db.Clients.ToList())
            {
                var lastOrder = client.Orders.OrderBy(x => x.MakeDate).FirstOrDefault();

                if (lastOrder == null || lastOrder.MakeDate < date)
                {
                    Client.Remove(client);
                }
            }
            /*
            var removeDate = DateTime.Now.AddMonths(-MONTHS_TO_REMOVE);
            var oldOrders = _db.Orders.Where(x => x.MakeDate <= removeDate && x.Archived);
            foreach (var oldOrder in oldOrders)
            {
                Order.Remove(oldOrder);
            }

            foreach (var client in _db.Clients.ToList())
            {
                var lastOrder = client.Orders.OrderBy(x => x.MakeDate).LastOrDefault();
                if (lastOrder == null || lastOrder.MakeDate <= removeDate)
                {
                    // Usunięcie z ilości kopii filmów tych zabranych przez klienta
                    foreach (var order in client.Orders)
                    {
                        if (!order.Archived)
                        {
                            foreach (var details in order.OrderDetails)
                            {
                                if (details.ReturnDate == null)
                                {
                                    --details.Movie.Quantity;
                                    Movie.Edit(details.Movie);
                                }
                            }
                        }
                    }

                    Client.Remove(client);
                }
            }*/
        }


        private DataSource()
        {
            _db = new MovieDbContext();
        }

    }
}
